//
//  ColorPickerViewController.m
//  ColorPicker
//
//  Created by Adam Cue on 1/18/10.
//  Copyright Adam Cue 2011.
//
//  This program is licensed and distributed under the terms of the GNU General Public License.  
//	The text for the license can be found in the license.txt file in this project.
//

#import "ColorPickerViewController.h"

#define COLOR_PICKER_FRAME          CGRectMake(8, 12, 231, 190)

@interface ColorPickerViewController (Private)
#pragma mark SetupMethods
- (void)setupColorPicker;
- (void)setupColorView;
@end

@implementation ColorPickerViewController

@synthesize picker = _picker;
@synthesize colorView = _colorView;
@synthesize colorPopover = _colorPopover;
@synthesize annotationViews = _annotationViews;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil annotationViews:(NSMutableArray *)selectedAnnotationViews{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.annotationViews = selectedAnnotationViews;
    }
    return self;
}

- (void)dealloc {
   self.picker = nil;
   self.colorView = nil;
   self.colorPopover = nil;
   self.annotationViews = nil;
   
   [super dealloc];
}

- (void)viewDidLoad {
   [super viewDidLoad];
	[self setupColorPicker];
   [self setupColorView];
	self.view.backgroundColor = [UIColor darkGrayColor];
}

#pragma mark SetupMethods
- (void)setupColorPicker {
   _picker = [[ANColorPicker alloc] initWithFrame:COLOR_PICKER_FRAME];
	[self.picker setDelegate:self];
	[self.view addSubview:self.picker];
}

- (void)setupColorView {
   CGRect frm = self.picker.frame;
	frm.origin.y += frm.size.height + 20;
	frm.size.height = 60;
	_colorView = [[UIView alloc] initWithFrame:frm];
	[self.colorView setBackgroundColor:[[self.annotationViews objectAtIndex:0] drawColor]];
	[self.view addSubview:self.colorView];
}

#pragma mark ANColorPickerDelegate
- (void)colorChanged:(UIColor *)newColor{
	UIColor *adjustedColor = [UIColor colorWithRed:CGColorGetComponents(newColor.CGColor)[0] green:CGColorGetComponents(newColor.CGColor)[1] blue:CGColorGetComponents(newColor.CGColor)[2] alpha:0.5];
	[self.colorView setBackgroundColor:adjustedColor];
	for(AnnotationView *annotationView in self.annotationViews){
		[annotationView setDrawColor:adjustedColor];
	}
}

- (void)colorsEnded{
	[self.colorPopover dismissPopoverAnimated:YES];
}

@end
